<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/27/0027
 * Time: 16:58
 */

namespace app\sports_admin\model;


use think\Model;

class Admin extends Model
{
    protected $table = 'admin';
    protected $pk = 'id';
    protected $hidden = ['last_time','add_time','pwd'];

    public function getPwdAttr($value)
    {
        $value = '';
        return $value;
    }
    public function adminRule()
    {
        return $this->hasOne('admin_rule','admin_id','id');
    }
    /**
     * 判断账号是否存在
     * @param $account 管理员账号
     */
    public static function findAdmin($account)
    {
        return self::where('login',$account)->find();
    }
    /**
     * 获取管理员权限
     * @param $adminId 管理员id
    */
    public static function getAdminRule($adminId)
    {
        return self::with('adminRule,adminRule.getRule')->find($adminId)->hidden(['pwd']);
    }
    /**
     * 获取管理员列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     */
    public static function getAdminInfo($page,$rows,$sort,$sortOrder,$searchName)
    {
        $adminList = self::with('adminRule,adminRule.getRule')->where(function($query)use($searchName){
            if($searchName){
                $query->where('name|login','like',"%{$searchName}%");
            }
        })->where('login','<>','admin')->page($page)->limit($rows)->order("{$sort}","{$sortOrder}")->select();
        $adminCount = self::with('adminRule,adminRule.getRule')->where(function($query)use($searchName){
            if($searchName){
                $query->where('name|login','like',"%{$searchName}%");
            }
        })->where('login','<>','admin')->count();
        return ['rows'=>$adminList,'total'=>$adminCount];
    }
}